package com.yiwei.jmx.view;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.yiwei.jmx.R;
import com.yiwei.jmx.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/3/16 0016.
 */
public class YmdLoadingDialog extends Dialog {


    @BindView(R.id.txt_number)
    TextView txtNumber;
    private Context context;

    public YmdLoadingDialog(Context context) {
        super(context, R.style.mydialog);
        this.context = context;
        init();
    }

    private static final int TIME = 5 * 1000;
    private CountDownTimer timer = new CountDownTimer(TIME, 1000) {

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onTick(long millisUntilFinished) {
            try {
                txtNumber.setText((millisUntilFinished / 1000) + "");
            } catch (Resources.NotFoundException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFinish() {
            if (selectListener != null) {
                selectListener.onSure();
            }
            dismiss();
        }
    };

    @Override
    public void setOnDismissListener(@Nullable OnDismissListener listener) {
        super.setOnDismissListener(listener);
        if (timer != null) {
            timer.cancel();
        }


    }


    public YmdLoadingDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public YmdLoadingDialog setOnSelect(OnSureListener listener) {
        selectListener = listener;
        return this;
    }

    @Override
    public void show() {
        super.show();
        timer.start();
    }


    public interface OnSureListener {
        void onSure();
    }

    private OnSureListener selectListener;

    private void init() {
        Window window = this.getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setWindowAnimations(R.style.mydialog_animation);
        setContentView(R.layout.dialog_ymd_loading);
        ButterKnife.bind(this);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = Util.getDisplayMetrics(context).heightPixels / 10 * 8;
        } else {
            lp.width = Util.getDisplayMetrics(context).widthPixels / 10 * 8;
        }
        window.setAttributes(lp);
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        this.setCanceledOnTouchOutside(false);

    }

}
